/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package presentation.view;

import java.awt.Color;
import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;
import presentation.controller.ControlPanelLogin;
import presentation.controller.ControlPanelRegister;
import vo.StudentVO;

/**
 *
 * @author Alexander
 */
public class PanelLogin extends javax.swing.JPanel {

    /**
     * Creates new form PanelLogin
     */
    public PanelLogin(MainFrame parent) {
        this.parent = parent;
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextFieldUsername = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jPasswordFieldUser = new javax.swing.JPasswordField();
        jButtonLogIn = new javax.swing.JButton();
        jButtonNewAccount = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();

        jLabel1.setFont(new java.awt.Font("Segoe UI Light", 0, 18)); // NOI18N
        jLabel1.setText("Usuario");

        jTextFieldUsername.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextFieldUsernameKeyReleased(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Segoe UI Light", 0, 18)); // NOI18N
        jLabel2.setText("Contraseña");

        jPasswordFieldUser.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jPasswordFieldUserKeyReleased(evt);
            }
        });

        jButtonLogIn.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jButtonLogIn.setText("Ingresar");
        jButtonLogIn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLogInActionPerformed(evt);
            }
        });

        jButtonNewAccount.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jButtonNewAccount.setForeground(new java.awt.Color(51, 51, 255));
        jButtonNewAccount.setText("Nueva cuenta local");
        jButtonNewAccount.setContentAreaFilled(false);
        jButtonNewAccount.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jButtonNewAccount.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jButtonNewAccountMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jButtonNewAccountMouseExited(evt);
            }
        });
        jButtonNewAccount.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNewAccountActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/presentation/view/images/Itineribusx128.png"))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPasswordFieldUser)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jButtonLogIn, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(24, 24, 24)
                        .addComponent(jButtonNewAccount))
                    .addComponent(jTextFieldUsername))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel1)
                .addGap(4, 4, 4)
                .addComponent(jTextFieldUsername, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPasswordFieldUser, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonLogIn)
                    .addComponent(jButtonNewAccount))
                .addContainerGap(55, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonLogInActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLogInActionPerformed
        StudentVO vo = new StudentVO();
        vo.setUsername(jTextFieldUsername.getText());
        vo.setPassword(new String(jPasswordFieldUser.getPassword()));
        StudentVO login = new ControlPanelLogin().login(vo);
        if (login != null) {
            JOptionPane.showMessageDialog(this, "Login Exitoso! Bienvenido " + jTextFieldUsername.getText(), "Aviso", JOptionPane.INFORMATION_MESSAGE, null);
            new ControlPanelRegister().modificarIp(vo);
            parent.controlBarraHerramientas(true, login.isAdministrator());
            if (login.isAdministrator()) {
                parent.cambiarPanel(null);
            } else {
                parent.cambiarPanel(MainPanel.getInstance());
            }
            parent.setResizable(true);
                                    
        } else {
            JOptionPane.showMessageDialog(this, "Datos incorrectos. Por favor revisa la información suministrada.", "Aviso", JOptionPane.INFORMATION_MESSAGE, null);
        }
    }//GEN-LAST:event_jButtonLogInActionPerformed

    private void jButtonNewAccountActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNewAccountActionPerformed
        parent.cambiarPanel(new PanelRegister(parent, "Registrar", null));
    }//GEN-LAST:event_jButtonNewAccountActionPerformed

    private void jButtonNewAccountMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonNewAccountMouseEntered
        jButtonNewAccount.setContentAreaFilled(true);
        jButtonNewAccount.setForeground(Color.red);
    }//GEN-LAST:event_jButtonNewAccountMouseEntered

    private void jButtonNewAccountMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonNewAccountMouseExited
        jButtonNewAccount.setContentAreaFilled(false);
        jButtonNewAccount.setForeground(Color.blue);
    }//GEN-LAST:event_jButtonNewAccountMouseExited

    private void jTextFieldUsernameKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldUsernameKeyReleased
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            jPasswordFieldUser.requestFocus();
        }
    }//GEN-LAST:event_jTextFieldUsernameKeyReleased

    private void jPasswordFieldUserKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jPasswordFieldUserKeyReleased
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            jButtonLogInActionPerformed(null);
        }
    }//GEN-LAST:event_jPasswordFieldUserKeyReleased
    
    private MainFrame parent;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonLogIn;
    private javax.swing.JButton jButtonNewAccount;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPasswordField jPasswordFieldUser;
    private javax.swing.JTextField jTextFieldUsername;
    // End of variables declaration//GEN-END:variables
}
